﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using Enterprise.Modules;
using System.Data.SqlClient;
using System.Configuration;

namespace Enterprise.Templates
{
    //Replacement for Region.ascx.cs.
    public class Region : WebControl, INamingContainer
    {
        private string _ID;
        private PlaceHolder phSection;

        public Region() { }

        public Region(string id)
        {
            this._ID = id;
            this.EnsureChildControls();
        }

        /// <summary>
        /// It has to be unique since this is inserted into a database as 
        /// identification for other usercontrols that this Region will hold.
        /// </summary>
        public string RegionID
        {
            get
            {
                return this._ID;
            }
            set
            {
                this._ID = value;
            }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }


        /// <summary>
        /// Get all modules that belong to this Region
        /// </summary>
        public List<IModule> LoadModules()
        {

            string PageID = HttpContext.Current.Request.Params["PageID"];
            
            if(string.IsNullOrEmpty(PageID))
                PageID = Config.GetStartPageID();

            List<IModule> moduleList = new List<IModule>();

            SqlConnection Conn = new SqlConnection(Config.ConnectionString());
            SqlCommand command = new SqlCommand(string.Format("SELECT * FROM Objects WHERE PageID={0} AND RegionID='{1}' AND PortalID=1 ORDER BY SortOrder", PageID, RegionID), Conn);
            
            Conn.Open();
            SqlDataReader dr = command.ExecuteReader();

            try
            {
                while (dr.Read())
                {
                    IModule module = ModuleLoader.GetInstance().LoadModule(dr["id"].ToString().Trim());
                    if(module!=null)
                        moduleList.Add(module);
                }
            }
          
            finally { Conn.Close(); }

            return moduleList;
            
        }

       

        /// <summary>
        /// Loads all modules in this Region
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Controls.Clear();
            Page.Response.Expires = 0;
            Page.Response.ExpiresAbsolute = DateTime.Now.AddMinutes(-1.0);
            Page.Response.CacheControl = "private";
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            phSection = new PlaceHolder();
            phSection.Controls.Clear();

            List<IModule> moduleList = LoadModules();

            foreach (IModule module in moduleList)
            {
                ModuleBase ctl = (ModuleBase)Page.LoadControl(module.UserControl);

                /*
                if (Page.User.Identity.IsAuthenticated && Page.User.IsInRole(Roles.Administrators.ToString()))
                {
                    UserControl admin = (UserControl)Page.LoadControl("~/Modules/SectionAdmin.ascx");
                    ((IModuleAdmin)admin).SectionControl = ctl;
                    phSection.Controls.Add(admin);
                }*/

                // if (section.IsOnline || (Page.User.IsInRole(RoleNames.Administrators.ToString())))
                // {
                phSection.Controls.Add(ctl);

                // }

                ctl.Module = module;
            }
            this.Controls.Add(phSection);
           

        }


    }
}
